package com.dian.remotesound;

import android.content.Intent;
import android.media.*;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;

public class MainActivity extends AppCompatActivity {

    private Socket socket;
    private AudioTrack audio;
    private Thread thread;
    private Handler handler;
    private boolean exit = false;
    private byte[] audiobuf = new byte[1024*1024];
    private InputStream socketin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button)findViewById(R.id.start);
        final TextView address = (TextView) findViewById(R.id.address);

        //最小缓存大小
        int bufsize = AudioTrack.getMinBufferSize(44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT);
        //构建一个AudioTrack
        audio = new AudioTrack(AudioManager.STREAM_MUSIC,
                44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT,
                bufsize,
                AudioTrack.MODE_STREAM
        );
        //启动
        audio.play();

        //处理socket消息
        handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case 1:
                        button.setText(R.string.connected);
                        button.setEnabled(false);
                        break;
                    case 2:
                        button.setText(R.string.connect);
                        button.setEnabled(true);
                        Snackbar.make(button, R.string.fail, Snackbar.LENGTH_LONG).show();
                        break;
                    default:
                        break;
                }
            }
        };

        //按钮点击
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try
                        {
                            socket = new Socket(address.getText().toString(),7555);
                            if(socket.isConnected())
                            {
                                Message msg = new Message();
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }
                            else
                                return;
                        }
                        catch (IOException e)
                        {
                            Message msg = new Message();
                            msg.what = 2;
                            handler.sendMessage(msg);
                            return;
                        }

                        try
                        {
                            socketin = socket.getInputStream();
                            while (!exit)
                            {
                                int num = socketin.read(audiobuf);
                                if(num > 88200)
                                    continue;
                                audio.write(audiobuf, 0, num);
                            }

                        }
                        catch (IOException e)
                        {
                            Message msg = new Message();
                            msg.what = 2;
                            handler.sendMessage(msg);
                        }



                    }
                });
                thread.start();
            }
        });
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        exit = true;
//        try
//        {
//            socket.close();
//        }
//        catch (IOException e)
//        {
//            e.printStackTrace();
//        }

    }

}
